<?php

class Admin_ReviewsController extends Zend_Controller_Action
{
    protected $dbMapper, $dbTable;


    public function init()
    {
        //@todo Убрать данный костыль автозагрузкой
        require_once(APPLICATION_PATH . '/modules/admin/models/DbMapper/Reviews.php');
        require_once(APPLICATION_PATH . '/modules/admin/models/DbTable/Reviews.php');

        $this->dbMapper = new Admin_Model_DbMapper_Reviews();
        $this->dbTable = new Admin_Model_DbTable_Reviews();
    }


    protected function prepareData($data)
    {
        $validData = array();
        if($this->_request->isPost()) {
            try {
                if($this->dbMapper->savePhoto()) {
                  $validData['photo_url'] = $this->dbMapper->getPhotoUrl();
                  $validData['small_photo_url'] = $this->dbMapper->getSmallPhotoUrl();
                }
            } catch (Exception $e) {
                $this->view->errorMessage = 'Возникла ошибка: ' . $e->getMessage();
            }

            foreach($data as $index => $value){
               if(in_array($index, $this->dbMapper->getColNames())) {
                   $validData[$index] = strip_tags($value);
               }
            }
        }
        return $validData;
    }

    public function indexAction()
    {
        $this->listAction();
    }

    public function listAction()
    {
        $this->view->reviews = $this->dbMapper->getList();
    }

    public function addAction()
    {
        if($this->_request->isPost()) {
            $data = $this->_request->getPost();
            $validData = $this->prepareData($data);
            $this->view->isInsert = (bool) $this->dbMapper->insert($validData);
        }
    }

    public function deleteAction()
    {
        $this->_helper->layout->disableLayout(); // layout
        $this->_helper->viewRenderer->setNoRender(); // action render

        if($id = $this->getParam('id', null)) {
            $this->dbMapper->fullDeleteById($id);
        }

        $this->_helper->redirector('list');
    }

    public function editAction()
    {
        if($id = $this->getParam('id', null)) {
            $this->view->review = $this->dbMapper->selectById($id);
        }

        if($this->_request->isPost()) {
            $data = $this->_request->getPost();
            $validData = $this->prepareData($data);

            //для checkbox
            if(!isset($validData['is_visible'])) {
                $validData['is_visible'] = 0;
            }

            //Удаляем фото
            if(isset($data['is_delete']) && $data['is_delete'] == 1) {
                unset($data['photo_url']);
                unset($data['small_photo_url']);

                $validData['photo_url'] = isset($validData['photo_url']) ? $validData['photo_url'] : '';
                $validData['small_photo_url'] = isset($validData['small_photo_url']) ? $validData['small_photo_url'] : '';
            }

            $this->view->isUpdate = $this->dbMapper->updateById($validData, $id);
            $this->_helper->redirector('list');
        }
    }
}

